﻿using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.Order
{
    public partial class OrderView : AuthBasePage
    {
        public DataTable OrderDetailDT
        {
            get
            {
                if (Session["OrderDetailDT"] == null)
                {
                    return CreateDetailDT();
                }
                else
                {
                    return (DataTable)Session["OrderDetailDT"];
                }
            }
            set
            {
                Session["OrderDetailDT"] = value;
            }
        }
        public DataTable CreateDetailDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("OrderNum");
            dt.Columns.Add("OrderDetailID");
            dt.Columns.Add("ProductID");
            dt.Columns.Add("ProductName");
            dt.Columns.Add("Unit");
            dt.Columns.Add("CustomerPartNo");
            dt.Columns.Add("Nums");
            dt.Columns.Add("Price");
            dt.Columns.Add("RMBPrice");
            dt.Columns.Add("Amount");
            dt.Columns.Add("TaxAmount");
            dt.Columns.Add("WorkOrderNo");
            dt.Columns.Add("IsFinish");
            dt.Columns.Add("IsNew");//是否新增
            dt.Columns.Add("IsDel");//是否删除
            return dt;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OrderDetailDT = null;
                InitData();
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }

        private void InitData()
        {
            string id = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id"));
            if (!string.IsNullOrEmpty(id))
            {
                DataTable dt = BsOrderBusiness.GetOrderData(id);
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    lblOrderID.Text = CommonMethod.GetString(dr["OrderID"]);
                    lblBillDate.Text = string.Format("{0:yyyy-MM-dd}", dr["BillDate"]);
                    lblQuoteID.Text = CommonMethod.GetString(dr["QuoteID"]);
                    lblCustomerID.Text = CommonMethod.GetString(dr["CustomerID"]);
                    lblCustomerName.Text = CommonMethod.GetString(dr["CustomerName"]);
                    lblContactName.Text = CommonMethod.GetString(dr["ContactName"]);
                    lblMobile.Text = CommonMethod.GetString(dr["Mobile"]);
                    lblSendCustomerName.Text = CommonMethod.GetString(dr["SendCustomerName"]);
                    lblAddr.Text = CommonMethod.GetString(dr["SendAddr"]);
                    lblSendContactName.Text = CommonMethod.GetString(dr["SendContactName"]);
                    lblSendMobile.Text = CommonMethod.GetString(dr["SendMobile"]);
                    lblDeliveryDate.Text = string.Format("{0:yyyy-MM-dd}", dr["DeliveryDate"]);
                    lblIsPreOrder.Text = CommonMethod.GetString(dr["IsPreOrder"]) == "1" ? "是" : "否";
                    lblOfficialOrder.Text = CommonMethod.GetString(dr["OfficialOrder"]);
                    string currency = CommonMethod.GetString(dr["Currency"]);
                    lblCurrency.Text = currency == "1" ? "是" : "否";
                    if (!string.IsNullOrEmpty(currency) && currency == CommonConsts.RMBCurrencyIndex)
                    {
                        TaxTr.Style.Remove("display");
                    }
                    lblIsTax.Text = CommonMethod.GetString(dr["IsTax"]) == "1" ? "是" : "否";
                    lblTaxRate.Text = CommonMethod.GetString(dr["TaxRate"]);

                    DataTable dtemp = OrderDetailDT.Copy();
                    foreach (DataRow drow in dt.Rows)
                    {
                        DataRow d = dtemp.NewRow();
                        d["OrderDetailID"] = CommonMethod.GetString(dr["OrderDetailID"]);
                        d["ProductID"] = CommonMethod.GetString(dr["ProductID"]);
                        d["ProductName"] = CommonMethod.GetString(dr["ProductName"]);
                        d["Unit"] = CommonMethod.GetString(dr["Unit"]);
                        d["CustomerPartNo"] = CommonMethod.GetString(dr["CustomerPartNo"]);
                        d["Nums"] = CommonMethod.GetString(dr["Nums"]);
                        d["Price"] = CommonMethod.GetString(dr["Price"]);
                        d["RMBPrice"] = CommonMethod.GetString(dr["RMBPrice"]);
                        d["Amount"] = CommonMethod.GetString(dr["Amount"]);
                        d["TaxAmount"] = CommonMethod.GetString(dr["TaxAmount"]);
                        d["WorkOrderNo"] = CommonMethod.GetString(dr["WorkOrderNo"]);
                        d["IsFinish"] = CommonMethod.GetString(dr["IsFinish"]) == "1" ? "是" : "否";
                        d["IsNew"] = "-1";
                        d["IsDel"] = "-1";
                        dtemp.Rows.Add(d);
                    }
                    OrderDetailDT = dtemp.Copy();
                }
            }
            BindData();
        }

        private void BindData()
        {
            DataView dv = new DataView(OrderDetailDT);
            dv.RowFilter = " IsDel <> '1' ";
            DataTable dt = dv.ToTable();
            if (dt != null && dt.Rows.Count > 0)
            {
                int index = 1;
                foreach (DataRow dr in dt.Rows)
                {
                    dr["OrderNum"] = index;
                    index++;
                }
                grdResult.DataSource = dt;
                grdResult.DataBind();
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }
    }
}